package com.woniu.after.controller;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.after.entity.TOrderAfter;
import com.woniu.after.entity.TOrderSource;
import com.woniu.after.entity.dto.OrderAfterDetailDto;
import com.woniu.after.entity.dto.OrderAfterDto;
import com.woniu.after.entity.dto.OrderNumberDetailDto;
import com.woniu.after.entity.dto.PageInfoDto;
import com.woniu.after.feign.OrderFeignListener;
import com.woniu.after.mapper.TOrderAfterMapper;
import com.woniu.after.service.TOrderAfterService;
import com.woniu.after.service.TOrderSourceService;
import com.woniu.after.util.MinioUtils;
import com.woniu.common.dto.MySkuOrderDto;
import com.woniu.common.dto.OrderDetailDto;
import com.woniu.common.util.PageParam;
import com.woniu.common.util.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.nio.pool.NIOConnFactory;
import org.apiguardian.api.API;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.repository.query.Param;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@Slf4j
@Api(tags = "售后订单API")
public class OrderAfterController {
    @Autowired
    private TOrderAfterService tOrderAfterService;
    @Autowired
    private TOrderSourceService orderSourceService;
    @Autowired
    private OrderFeignListener orderFeignListener;
    @Autowired
    private TOrderAfterMapper orderAfterMapper;
    @Autowired
    private MinioUtils minioUtils;

    /**
     * 查询所有售后订单
     */
    @PostMapping("queryAllAfterOrder")
    @ApiOperation(value = "获取售后订单信息", notes = "售后订单信息")
    public ResponseData<?> queryAllAfterOrder(@RequestBody PageInfoDto pageInfoDto) {
        Page<OrderAfterDto> page = new Page<>(pageInfoDto.getPageNum(), pageInfoDto.getPageSize());
        IPage<OrderAfterDto> orderAfterDtoIPage = tOrderAfterService.queryAllAfterOrderInfo(page, pageInfoDto);
        return new ResponseData<>().ok(orderAfterDtoIPage);
    }

    /**
     * 查询订单源类型数据
     */
    @GetMapping("querySourceInfo")
    @ApiOperation(value = "获取订单源信息", notes = "获取订单源信息")
    public ResponseData<?> querySourceInfo() {
        List<TOrderSource> list = orderSourceService.query().list();
        return new ResponseData<>().ok(list);
    }

    //    创建售后订单
    @PostMapping("createAfterOrder")
    @ApiOperation(value = "生成售后订单", notes = "生成售后订单")
    public ResponseData<?> createAfterOrder(@RequestBody List<TOrderAfter> tOrderAfter) throws Exception {
        tOrderAfterService.createAfterOrder(tOrderAfter);
        return new ResponseData<>().ok();
    }



    //    查询子订单售后详情
    @GetMapping("queryAfterDetailInfo")
    @ApiOperation(value = "查询子订单售后详情", notes = "查询子订单售后详情")
    public ResponseData<?> queryAfterDetailInfo(Long detailNum) {
        OrderAfterDetailDto orderAfterDetailDto = tOrderAfterService.queryAfterDetailInfo(detailNum);
        return new ResponseData<>().ok(orderAfterDetailDto);
    }

//    根据子订单查询售后信息
    @GetMapping("queryByAfterInfoOther")
    @ApiOperation(value = "查询子订单售后其他信息", notes = "查询子订单售后其他信息")
    public ResponseData<?> queryByAfterInfoOther(Long detailNum){
        OrderDetailDto orderDetailDto = orderFeignListener.queryOrderDetailById(detailNum);
        return new ResponseData<>().ok(orderDetailDto);
    }

    @GetMapping("queryByAfterNum")
    @ApiOperation(value = "根据售后单号查询订单信息", notes = "根据售后单号查询订单信息")
    public ResponseData<?> queryByAfterNum(Long afterNum){
        OrderNumberDetailDto orderNumberDetailDto = tOrderAfterService.queryByAfterNum(afterNum);
        return new ResponseData<>().ok(orderNumberDetailDto);
    }

//    修改售后信息状态
    @GetMapping("changeAfterNumStatus")
    @ApiOperation(value = "修改售后订单状态", notes = "修改售后订单状态")
    public ResponseData<?> changeAfterNumStatus(Long detailNum){
        tOrderAfterService.changeAfterNumStatus(detailNum);
        return new ResponseData<>().ok();
    }
    /*
    Excel 批量导出
     */
    @PostMapping("exprotAfterExcel")
    @ApiOperation(value = "Excel批量导出", notes = "Excel批量导出")
    public void exprotAfterExcel(@RequestBody List<String> detailNums, HttpServletResponse response){
        tOrderAfterService.exprotAfterExcel(detailNums,response);
    }
    @GetMapping("queryOrderOk")
    @ApiOperation(value = "查询已收货", notes = "已收获订单")
    public  ResponseData<?> queryOrderOk(String phone){
        List<MySkuOrderDto> mySkuOrderDtos = orderAfterMapper.queryOrderOK(phone);
        return new ResponseData<>().ok(mySkuOrderDtos);

    }
}
